package com.jbm.Community.Frame;

import java.util.ArrayList;

import com.jbm.Community.R;
import com.jbm.Community.Frame.HelpPiazzaFragment.InfoHolder;
import com.jbm.jbmsupplier.model.Address;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class SelectAddressActivity extends Activity {

	@SuppressWarnings("unused")
	private ListView mlvAddress = null;
	@SuppressWarnings("unused")
	private Button mbtNewAddress = null;
	@SuppressWarnings("unused")
	private ArrayList<Address> mAddressArray = new ArrayList<Address>();
	private AddressAdapter mAdapter = new AddressAdapter();
	
	public static Address currentAddress = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_address);
		
		this.mlvAddress = (ListView)this.findViewById(R.id.address_list);
		this.mbtNewAddress = (Button)this.findViewById(R.id.new_address_button);
		
		this.mlvAddress.setAdapter(this.mAdapter);
		this.mlvAddress.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Address address = (Address)arg1.getTag();
				if(address != null)
					currentAddress = address;
			}});
		
		this.mbtNewAddress.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				NewAddress();
			}});
	}
	
	protected void NewAddress() {
		
	}

	private class AddressAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return mAddressArray.size();
		}

		@Override
		public Object getItem(int position) {
			return mAddressArray.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView != null)
				return convertView;
			convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.select_address_item, null);
			TextView tvContacts = (TextView)convertView.findViewById(R.id.address_contacts);
			TextView tvPhone = (TextView)convertView.findViewById(R.id.address_phone);
			TextView tvAddress = (TextView)convertView.findViewById(R.id.address_address);
			
			Address address = mAddressArray.get(position);
			tvContacts.setText(address.mContacts);
			tvPhone.setText(address.mPhone);
			tvAddress.setText(address.mAddress);
			convertView.setTag(address);
			return convertView;
		}
		
	}
}
